package com.jyy.prefabricated.service;

import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.entity.ManageRuleCategory;
import com.jyy.prefabricated.enums.BaseStatusEnum;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.ManageRuleCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class ManageRuleCategoryService {

    @Autowired
    private ManageRuleCategoryMapper manageRuleCategoryMapper;

    /**
     * 新增
     * @param manageRuleCategory
     * @return
     */
    public int insert(ManageRuleCategory manageRuleCategory){
        manageRuleCategory.setStatus(BaseStatusEnum.INVALID.getValue());
        manageRuleCategoryMapper.insert(manageRuleCategory);
        return manageRuleCategory.getId();
    }

    /**
     * 更新
     * @param manageRuleCategory
     * @return
     */
    public int update(ManageRuleCategory manageRuleCategory){
        if(manageRuleCategory.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        return manageRuleCategoryMapper.updateByPrimaryKeySelective(manageRuleCategory);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        return manageRuleCategoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 批量删除
     * @param dto
     * @return
     */
    public int batchDelete(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return manageRuleCategoryMapper.batchDelete(dto.getIds());
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public ManageRuleCategory selectById(Integer id){
        return manageRuleCategoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有分类
     * @return
     */
    public List<ManageRuleCategory> selectAll(){
        return manageRuleCategoryMapper.selectAll();
    }

    /**
     * 根据参数查询列表
     * @param category 名称
     * @param pid 父级id
     * @param status 状态
     * @return
     */
    public List<ManageRuleCategory> selectByParams(String category, Integer pid, Integer status){
        return manageRuleCategoryMapper.selectByParams(category, pid, status);
    }


    /**
     * 批量更新状态
     * @param dto
     * @return
     */
    public int batchUpdateStatus(BatchDto dto){
        if(dto.getIds().size() == 0){
            throw new CustomException("参数id不能为空");
        }
        return manageRuleCategoryMapper.batchUpdateStatus(dto.getIds(), dto.getStatus());
    }

}
